package com.oristartech.netty.common.message;

import java.io.Serializable;

/**
 ** 消息抽象对象
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-03-18 14:34
 * @blame Team
 */
public abstract class AbstractMessage implements Serializable {

    private static final long serialVersionUID = 8403762797529616861L;

    /**
     ** Google Protocol Buffer数据对象
     */
    private GoogleProtobuf.DataObject dataObject;

    /**
     ** 获取消息ID
     *
     * @return String
     */
    public abstract String getId();

    /**
     ** 获取消息类型
     *
     * @return MessageType
     */
    public abstract MessageType getType();

    /**
     ** 获取租户ID
     *
     * @return String
     */
    public abstract String getTenantId();

    /**
     ** 获取影院编码
     *
     * @return String
     */
    public abstract String getCinemaCode();

    /**
     ** 获取服务名称
     *
     * @return String
     */
    public abstract String getServiceName();

    /**
     ** 获取方法名称
     *
     * @return String
     */
    public abstract String getMethodName();

    /**
     ** 获取消息内容
     *
     * @return String
     */
    public abstract String getContent();

    /**
     ** 获取时间戳
     *
     * @return String
     */
    public abstract long getTimestamp();

    /**
     ** 判断消息类型
     *
     * @return boolean
     */
    public boolean isMessageType(MessageType type){
        if(getType() == null){
            return false;
        }
        return getType().compare(type);
    }

    /**
     ** 转换为Google Protocol Buffer数据对象
     *
     * @return Google Protocol Buffer数据对象
     */
    public GoogleProtobuf.DataObject toDataObject(){
        if(dataObject == null){
            dataObject = GoogleProtobuf.DataObject.newBuilder()
                    .setId(getId())
                    .setType(getType().getCode())
                    .setTenantId(getTenantId())
                    .setCinemaCode(getCinemaCode())
                    .setTimestamp(getTimestamp())
                    .build();
            if(getServiceName() != null){
                dataObject = dataObject.toBuilder().setServiceName(getServiceName()).build();
            }
            if(getMethodName() != null){
                dataObject = dataObject.toBuilder().setMethodName(getMethodName()).build();
            }
            if(getContent() != null){
                dataObject = dataObject.toBuilder().setContent(getContent()).build();
            }
        }
        return dataObject;
    }
}
